﻿using System;
using System.Linq;
using UnitFundProfitability.DataMigrationTool.NewDatabase;
using UnitFundProfitability.DataMigrationTool.OldDatabase;

namespace UnitFundProfitability.DataMigrationTool
{
	class Program
	{
		static void Main(string[] args)
		{
			OldDatabaseEntities oldContext = new OldDatabaseEntities();
			NewDatabaseEntities newContext = new NewDatabaseEntities();
			MigrateExpenditures(oldContext, newContext);
			MigrateIncomes(oldContext, newContext);
			MigrateManagementCompanies(oldContext, newContext);
			MigrateMarkdownRates(oldContext, newContext);
			MigrateMarkupRates(oldContext, newContext);
			MigrateOperations(oldContext, newContext);
			MigrateOperationTypes(oldContext, newContext);
			MigrateUnitFunds(oldContext, newContext);
			MigrateUnitPrices(oldContext, newContext);
			Console.WriteLine("Saving changes...");
			newContext.SaveChanges();
			Console.WriteLine("Completed!");
			Console.ReadLine();
		}

		private static void MigrateUnitPrices(OldDatabaseEntities oldContext, NewDatabaseEntities newContext)
		{
			Console.WriteLine("Migrating unit prices...");
			foreach (OldDatabase.UnitPrice oldUnitPrice in oldContext.UnitPrices)
			{
				NewDatabase.UnitPrice newUnitPrice = new NewDatabase.UnitPrice();
				newUnitPrice.FundId = oldUnitPrice.FundId;
				newUnitPrice.Price = oldUnitPrice.Price;
				newUnitPrice.PriceDate = oldUnitPrice.PriceDate;
				newUnitPrice.UnitPriceId = oldUnitPrice.UnitPriceId;
				newContext.UnitPrices.AddObject(newUnitPrice);
			}
		}

		private static void MigrateUnitFunds(OldDatabaseEntities oldContext, NewDatabaseEntities newContext)
		{
			Console.WriteLine("Migrating unit funds...");
			foreach (OldDatabase.UnitFund oldUnitFund in oldContext.UnitFunds)
			{
				NewDatabase.UnitFund newUnitFund = new NewDatabase.UnitFund();
				newUnitFund.CompanyId = oldUnitFund.CompanyId;
				newUnitFund.FundId = oldUnitFund.FundId;
				newUnitFund.FundName = oldUnitFund.FundName;
				newContext.UnitFunds.AddObject(newUnitFund);
			}
		}

		private static void MigrateOperationTypes(OldDatabaseEntities oldContext, NewDatabaseEntities newContext)
		{
			Console.WriteLine("Migrating operation types...");
			foreach (OldDatabase.OperationType oldOperationType in oldContext.OperationTypes)
			{
				NewDatabase.OperationType newOperationType =
					(from item in newContext.OperationTypes
					 where item.OperationTypeId == oldOperationType.OperationTypeId
					 select item).SingleOrDefault();
				bool isAdding = false;
				if (newOperationType == null)
				{
					isAdding = true;
					newOperationType = new NewDatabase.OperationType();
					newOperationType.OperationTypeId = oldOperationType.OperationTypeId;
				}
				newOperationType.OperationTypeName = oldOperationType.OperationTypeName;
				if (isAdding)
				{
					newContext.OperationTypes.AddObject(newOperationType);
				}
			}
		}

		private static void MigrateOperations(OldDatabaseEntities oldContext, NewDatabaseEntities newContext)
		{
			Console.WriteLine("Migrating operations...");
			foreach (OldDatabase.Operation oldOperation in oldContext.Operations)
			{
				NewDatabase.Operation newOperation = new NewDatabase.Operation();
				newOperation.IsPendingOperation = false;
				newOperation.OperationDate = oldOperation.OperationDate;
				newOperation.OperationId = oldOperation.OperationId;
				newOperation.OperationTypeId = oldOperation.OperationTypeId;
				newContext.Operations.AddObject(newOperation);
			}
		}

		private static void MigrateMarkupRates(OldDatabaseEntities oldContext, NewDatabaseEntities newContext)
		{
			Console.WriteLine("Migrating markup rates...");
			foreach (OldDatabase.MarkupRate oldMarkupRate in oldContext.MarkupRates)
			{
				NewDatabase.MarkupRate newMarkupRate = new NewDatabase.MarkupRate();
				newMarkupRate.Date = oldMarkupRate.Date;
				newMarkupRate.FundId = oldMarkupRate.FundId;
				newMarkupRate.MarkupRateId = oldMarkupRate.MarkupRateId;
				newMarkupRate.Rate = oldMarkupRate.Rate;
				newContext.MarkupRates.AddObject(newMarkupRate);
			}
		}

		private static void MigrateMarkdownRates(OldDatabaseEntities oldContext, NewDatabaseEntities newContext)
		{
			Console.WriteLine("Migrating markdown rates...");
			foreach (OldDatabase.MarkdownRate oldMarkdownRate in oldContext.MarkdownRates)
			{
				NewDatabase.MarkdownRate newMarkdownRate = new NewDatabase.MarkdownRate();
				newMarkdownRate.Date = oldMarkdownRate.Date;
				newMarkdownRate.FundId = oldMarkdownRate.FundId;
				newMarkdownRate.MarkdownRateId = oldMarkdownRate.MarkdownRateId;
				newMarkdownRate.Rate = oldMarkdownRate.Rate;
				newContext.MarkdownRates.AddObject(newMarkdownRate);
			}
		}

		private static void MigrateManagementCompanies(OldDatabaseEntities oldContext, NewDatabaseEntities newContext)
		{
			Console.WriteLine("Migrating management companies...");
			foreach (OldDatabase.ManagementCompany oldManagementCompany in oldContext.ManagementCompanies)
			{
				NewDatabase.ManagementCompany newManagementCompany = new NewDatabase.ManagementCompany();
				newManagementCompany.CompanyId = oldManagementCompany.CompanyId;
				newManagementCompany.CompanyName = oldManagementCompany.CompanyName;
				newContext.ManagementCompanies.AddObject(newManagementCompany);
			}
		}

		private static void MigrateIncomes(OldDatabaseEntities oldContext, NewDatabaseEntities newContext)
		{
			Console.WriteLine("Migrating incomes...");
			foreach (OldDatabase.Income oldIncome in oldContext.Incomes)
			{
				NewDatabase.Income newIncome = new NewDatabase.Income();
				newIncome.FundId = oldIncome.FundId;
				newIncome.IncomeId = oldIncome.IncomeId;
				newIncome.MoneyAmount = oldIncome.MoneyAmount;
				newIncome.UnitsAmount = oldIncome.UnitsAmount;
				newIncome.UnitsExpenditureAmount = oldIncome.UnitsExpenditureAmount;
				newContext.Incomes.AddObject(newIncome);
			}
		}

		private static void MigrateExpenditures(OldDatabaseEntities oldContext, NewDatabaseEntities newContext)
		{
			Console.WriteLine("Migrating expenditures...");
			foreach (OldDatabase.Expenditure oldExpenditure in oldContext.Expenditures)
			{
				NewDatabase.Expenditure newExpenditure = new NewDatabase.Expenditure();
				newExpenditure.ExpenditureId = oldExpenditure.ExpenditureId;
				newExpenditure.FundId = oldExpenditure.FundId;
				newExpenditure.MoneyAmount = oldExpenditure.MoneyAmount;
				newExpenditure.UnitsAmount = oldExpenditure.UnitsAmount;
				newContext.Expenditures.AddObject(newExpenditure);
			}
		}
	}
}
